package com.zzt.appframe8.widget;

import android.annotation.SuppressLint;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.ViewCompat;

import com.google.android.material.internal.CheckableImageButton;
import com.google.android.material.internal.TextWatcherAdapter;
import com.zzt.appframe8.R;

import java.util.Arrays;

/**
 * @author: zeting
 * @date: 2022/9/1
 * 清除按钮工具
 */
@SuppressLint("RestrictedApi")
public class ClearEndIconDelegate extends EndIconDelegate {

    private CheckableImageButton imageBtn; // 当前可操作按钮
    private boolean enableImgBtn;// 当前按钮是否可操作状态
    private final TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            EditText editText = textInputLayout.getEditText();
            if (editText == null) {
                return;
            }
            boolean hasFocus = editText.hasFocus();
            if (hasFocus && s.length() > 0) {
                setEnable(true);
            } else {
                setEnable(false);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    ClearEndIconDelegate(@NonNull MTextInputLayout textInputLayout) {
        super(textInputLayout);
    }


    @Override
    public void initialize() {
        if (!enableImgBtn) {
            return;
        }
        if (imageBtn != null) {
            Drawable drawable = imageBtn.getDrawable();
            if (drawable == null) {
                imageBtn.setImageDrawable(AppCompatResources.getDrawable(context, android.R.drawable.ic_delete));
                refreshIconDrawableState(imageBtn, AppCompatResources.getColorStateList(context, R.color.m_text_btn_text_color_selector));
            }
            EditText editText = textInputLayout.getEditText();
            if (editText != null) {
                boolean hasFocus = editText.hasFocus();
                if (hasFocus && editText.getText().length() > 0) {
                    setEnable(true);
                } else {
                    setEnable(false);
                }
                editText.removeTextChangedListener(textWatcher);
                editText.addTextChangedListener(textWatcher);
                imageBtn.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EditText editText = textInputLayout.getEditText();
                        if (editText == null) {
                            return;
                        }
                        editText.setText("");
                        setEnable(false);
                    }
                });
                setIconClickable(imageBtn);
            }
        }
    }

    @Override
    void setTintIconOnError(int mErrorTextColor) {
        tintEndIconOnError(mErrorTextColor);
    }

    /**
     * 设置错误时候颜色
     */
    private void tintEndIconOnError(int mErrorTextColor) {
        if (imageBtn != null && imageBtn.getDrawable() != null) {
            // Setting the tint here instead of calling setEndIconTintList() in order to preserve and
            // restore the icon's original tint.
            Drawable endIconDrawable = DrawableCompat.wrap(imageBtn.getDrawable()).mutate();
            DrawableCompat.setTint(endIconDrawable, mErrorTextColor);
            imageBtn.setImageDrawable(endIconDrawable);
        }
    }

    /**
     * 获取 View 的焦点监听
     *
     * @param v
     * @param hasFocus
     */
    public void onFocusChange(View v, boolean hasFocus) {
        EditText editText = textInputLayout.getEditText();
        if (editText == null) {
            return;
        }
        if (hasFocus && editText.getText().length() > 0) {
            setEnable(true);
        } else {
            setEnable(false);
        }
    }


    public void setImageBtnEnable(Boolean enableImageBtn, CheckableImageButton imageButton) {
        this.enableImgBtn = enableImageBtn;
        if (enableImgBtn) {
            this.imageBtn = imageButton;
            imageBtn.setVisibility(enableImageBtn ? View.VISIBLE : View.GONE);
        } else {
            imageButton.setVisibility(View.GONE);
        }
    }

    public void setEnable(Boolean enableImageBtn) {
        if (imageBtn != null) {
            imageBtn.setVisibility(enableImageBtn ? View.VISIBLE : View.GONE);
        }
    }


    public ClearEndIconDelegate setIconDrawable(@Nullable Drawable endIconDrawable) {
        if (imageBtn != null) {
            imageBtn.setImageDrawable(endIconDrawable);
        }
        return this;
    }


}
